#include <iostream>
#include "userservice_impl.h"
#include "book.h"
#include "reader.h"
#include "tools.h"
#include <vector>
#include "userdao.h"
#include "userdao_impl.h"
#include<iomanip>
#include<getch.h>
#include <algorithm>
#include <map>

using namespace std;
vector<Book> books;
vector<Reader> readers;
vector<Remark> remarks;

UserServiceImpl::UserServiceImpl()
{
	r_dao = new UserDaoImpl;
	b_dao = new BookDaoImpl;
	re_dao = new RemarkDaoImpl;
}

void UserServiceImpl::ReaderSearch(int id)
{
	r_dao->load(readers);
	cout << left << setw(23) << "姓名" << left << setw(19) << "id" << left << setw(20) << "预留金额" << endl;
	for(unsigned int i=0;i<readers.size();i++)
	{
	    if(readers[i].get_id() == id)
	    {
	        readers[i].show();
			//在读书籍
		    for(unsigned int j=0;j<readers[i].books.size();j++)
		    {
		    	cout << "第" << j+1 << "本书，书编号:";
		    	cout << readers[i].books[j].GetId() << "    ";
		    	cout << "书的名字:" <<readers[i].books[j].GetName() << endl;
		    }
			cout << "====================================================" << endl;
			//历史书籍
			cout << "历史书籍列表：" << endl;
			unsigned int j=0;
			for(;j<readers[i].bookid.size();j++)
			{
				cout << "第" << j+1 << "本书，书编号:";
				cout << readers[i].bookid[j] << endl;
			}
			cout << "****************************************************" << endl;
			cout << "      请给你心爱的书籍来条评语吧！" << endl;
		    cout << "        q、忍心退出" << endl;
			getch();
			int cmd;
			cmd = getch();
		    if('q' != cmd)
		    {
		        RemarkRun(i);
		    }	
    	}
	} 
	r_dao->save(readers);
}
void UserServiceImpl::RemarkRun(int i)
{
    while(1)
	{
		re_dao->load(remarks);
		system("clear");
		//历史书籍
		cout << "历史书籍列表：" << endl;
		unsigned int j=0;
		for(;j<readers[i].bookid.size();j++)
		{
			cout << "*******第" << j+1 << "本书，书编号:";
			cout << readers[i].bookid[j] << endl;
		}
		cout <<  "9.退出" << endl;
		cout << "请输入是第几本书：";
		char cmd = 0;
		char max_book = readers[i].bookid.size() +'0';
		while(1)
		{
			cmd = getch();
			if(cmd == '9') return;
			if(cmd >='1' && cmd <= max_book)
				break;
		}
		cout << cmd << endl;
		int num = cmd - '0';
		int reader_id = readers[i].get_id();
		int book_id = readers[i].bookid[num-1];
		char buf[255];
		cout << "请输入评语：";
		cin >> buf;
		Remark* remark = new Remark(book_id,reader_id,buf);
		//容器
		remarks.push_back(*remark);
		cout << "  书评：" << remarks[0].GetBuf() << endl;
		cout << remarks.size();
		re_dao->save(remarks);
 	}
}
void UserServiceImpl::BookSearch(char *str)
{
	books.clear();
	remarks.clear();
	re_dao->load(remarks);
	b_dao->load(books);
	cout << remarks.size() << endl;
	cout << left << setw(20) << "书的ID" << left << setw(23) << "书的名字" << left << setw(23) << "是否出借" << left << setw(23) << "是否预约" << left << setw(28) << "借阅人数" << "评分"<< endl;
	for(unsigned int i=0;i<books.size();i++)
	{
		if(check_book(books.at(i).GetName(),str))
		{
		books[i].AllShow();
		for(unsigned int j=0;j<remarks.size();j++)
		{
		//	cout << remarks[j].GetBookId() << endl;
			if(remarks[j].GetBookId() == books[i].GetId())
			{
				cout << remarks[j].GetReaderId() << ":";
				cout << remarks[j].GetBuf() << endl;
			}
		}
		}
	}
}

bool UserServiceImpl::BookOrder(int r_id,int b_id)
{
    
    b_dao->load(books);
	for(unsigned int i=0;i<books.size();i++)
	{
	    if(b_id == books[i].GetId())
		{
		    cout << books[i].GetId() << endl;
		    if(books[i].GetBorrow()) 
		    {
		        cout << books[i].GetBorrow() << endl;
		        if(books[i].GetAppoint() == 0)
		        {
		            cout << books[i].GetAppoint() << endl;
		            books[i].SetAppoint(r_id);
		            cout << books[i].GetAppoint() << endl;
		            b_dao->save(books);
		            return true;
		        }
		    }
		}
	} 
	return false;
}
void UserServiceImpl::Recommend()
{
    while(1)
	{
		system("clear");
		cout << "*************推荐***************" << endl;
		cout << "        1、按借阅次数排名推荐" << endl;
		cout << "        2、按评分排名推荐" << endl;
		cout << "        3、退出" << endl;
		cout << "*********************************" << endl;
		char cmd = 0;
		while(1)
		{
			cmd = getch();
			if(cmd >='1' && cmd <='3')
				break;
		}
		cout << cmd << endl;
		switch(cmd-'0')
		{
			case 1:Cnt();break;
			case 2:Soc();break;
			case 3:return;
			
		}
 	}
}

bool UserServiceImpl::Compare1(Book b1,Book b2)
{
    if(b1.GetCnt() > b2.GetCnt())
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool UserServiceImpl::Compare2(Book b1,Book b2)
{
    if(b1.GetScore() > b2.GetScore())
    {
        return true;
    }
    else
    {
        return false;
    }
}
void UserServiceImpl::Cnt()
{
    books.clear();
	b_dao->load(books);
	sort(books.begin(),books.end(),UserServiceImpl::Compare1);
	map<string,int>m;
    cout << "借阅次数前三图书" << endl;
	unsigned int i = 0;
	int count = 0;
	for(i = 0;i < books.size();i++)
	{
		if(3 <= count)
		{
			break;
		}
        if(0 != m[books[i].m_name])
		{
			continue;
		}
		++m[books[i].m_name];
		
		cout << "书名：" << books[i].m_name << "  编号：" << books[i].m_id << "  借阅次数：" << books[i].GetCnt() << endl;
			
		++count;
	}
	anykey_continue();
}
void UserServiceImpl::Soc()
{
    books.clear();
	b_dao->load(books);
	sort(books.begin(),books.end(),UserServiceImpl::Compare2);
	map<string,int>m;
    cout << "评分前三图书" << endl;
	unsigned int i = 0;
	int count = 0;
	for(i = 0;i < books.size();i++)
	{
		if(3 <= count)
		{
			break;
		}
        if(0 != m[books[i].m_name])
		{
			continue;
		}
		++m[books[i].m_name];
		
		cout << "书名：" << books[i].m_name << "  编号：" << books[i].m_id << "  评分：" << books[i].GetScore() << endl;
			
		++count;
	}
	anykey_continue();
}
UserServiceImpl::~UserServiceImpl()
{
	delete r_dao;
	delete b_dao;
}
